// 搜索页面相关配置
import api from "@/api/index"
import { getPMCList } from '@/js/scripts'

export default {
    namespaced: true,
    actions: {
        // 搜索
        search(context, payload) {
            const { term, select } = payload;

            // 修改状态
            context.state.currentPage = 1;
            context.state.isLoading = true;

            if (select == "PMC") {
                api.downloadApi.downloadByTerm(term).then(
                    response => {
                        context.dispatch('doResponse', { response: response, method: "search" });
                    },
                    error => {
                        console.log("请求失败", error.message);
                        context.state.isLoading = false;
                    }
                )
            }
            else if (select == "Django") {
                api.downloadApi.downloadFromDjango().then(
                    response => {
                        context.dispatch('doResponse', { response: response, method: "search" });
                    },
                    error => {
                        console.log("请求失败", error.message);
                        context.state.isLoading = false;
                    }
                )
            }
            else {
                alert("搜索未选择！");
                context.state.isLoading = false;
            }
        },

        // 翻页
        turnPage(context, payload) {
            const { select } = payload;
            if (select == "Django") return;
            let page = context.state.currentPageParm + 1;

            // 修改状态
            context.state.isLoading = true;

            // 请求翻页
            api.downloadApi.downloadByTurnPage(page).then(
                response => { 
                    context.dispatch('doResponse', { response: response, method: "turnPage" });
                    console.log(context.state);
                },
                error => {
                    console.log("请求失败", error.message);
                    context.state.isLoading = false;
                },
            );
            // 修改状态
            context.state.currentPageParm += 1; // 增加一页
        },

        // 请求数据成功后的response
        doResponse(context, payload) {
            const { response, method } = payload;// 返回数据
            console.log("请求成功！", response);

            let dataList = response.data.items; // Django或PMC传回的数据列表

            // 去数据库比对数据状态
            let pmcList = getPMCList(dataList);
            // 调用API
            api.databaseApi.selectState(pmcList).then(
                response => {
                    // 数据库返回[{PMDID : ,state: },{},{},...]
                    let dbList = response.data.dataList;
                    // 依据数据库修改状态
                    for (let i = 0; i < dbList.length; i++) {
                        for (let j = 0; j < dataList.length; j++) {
                            if (dbList[i].PMCID == dataList[j].PMCID) {
                                dataList[j].state = dbList[i].state;
                                break;
                            }
                        }
                    }
                }
            )

            if (method == "search") {
                // 提交添加的数据(覆盖当前列表)
                context.commit('COVER', { list: dataList }, { root: true });
            }
            else if (method == "turnPage") {
                context.commit('ADD', { list: dataList }, { root: true });
            }

            // 恢复可操作性状态
            setTimeout(() => {
                context.state.isLoading = false;
            }, 750);


        },
    },

    mutations: {
        // 改变当前页数
        CurrentPageChange(state, value) {
            state.currentPage = value;
        },
        // 改变每页行数
        PageSizeChange(state, value) {
            state.pageSize = value;
        },
        // 更新数据列表
        UpdateTextList(state, value) {

            // 过滤出带有文章信息的对象
            state.textList = value.filter((i) => i["PMCID"]);

            for (let i = 0; i < state.textList.length; i++) {
                state.textList[i].number = String(i + 1);// 对数据加上序号
                state.textList[i].state = 1; // 表示未提取状态
            }
        },
        // 状态转换
        ChangeState(state, payload) {
            state.textList.find(i => { return i.PMCID == payload.scope.row.PMCID }).state = payload.newState;
        },
        // 全部提取
        ChangeAllState(state, payload) {
            state.textList.forEach(i => { i.state = payload.newState });
        },

        // 每页行数setter
        SetPageSize(state,value){
            state.pageSize = parseInt(value);
        }
    },

    state: {
        isLoading: false, // 是否处于加载状态
        currentPage: 1, // 当前页数 -- 表格显示
        currentPageParm: 1, // 表示后端实际的页码参数 -- API请求时传入参数
        pageSize: 10, //每页行数
    },

    getters: {
        // 显示存放数据的列表
        textList(a, b, rootState) {
            return rootState.dataList;
        },

        // 没有下载(state==1)的数据列表
        notDownloadList(state, getters) {
            return getters.textList.filter(i => i.state == 1);
        },

        // 准备下载状态数据
        readyToDownloadList(state, getters) {
            return getters.textList.filter(i => i.state == 2)
        },
    },
}